package com.zzh.myapplication.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import net.icarplus.car.R;
import net.icarplus.car.bean.CarImageBean;
import net.icarplus.car.bean.CarInfo;
import net.icarplus.car.bean.CarPriceBean;
import net.icarplus.car.constant.UrlValues;

import java.util.List;

/**
 * 项目名称：icarplus-car-android
 * 类描述：
 * 创建人：Gophy
 * 创建时间：16/3/8 16:15
 * 修改人：Gophy
 * 修改时间：16/3/8 16:15
 * 修改备注：
 */


public class CarListAdapter extends BaseAdapter {

    private Context context;

    private LayoutInflater inflater;
    private List<CarInfo> carList;

    public CarListAdapter(Context context, List<CarInfo> carList) {
        super();
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.carList = carList;
    }

    @Override
    public int getCount() {
        return carList == null ? 0 : carList.size();
    }

    @Override
    public Object getItem(int i) {
        return carList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            view = inflater.inflate(R.layout.item_car_list, null);
            viewHolder.carPic = (SimpleDraweeView) view.findViewById(R.id.iv_car_name);
            viewHolder.carName = (TextView) view.findViewById(R.id.tv_car_name);
            viewHolder.carPrice = (TextView) view.findViewById(R.id.tv_car_money);
            viewHolder.carUnit = (TextView) view.findViewById(R.id.tv_per_min);
            viewHolder.carEndurance = (TextView) view.findViewById(R.id.tv_car_endurance);
            viewHolder.carNum = (TextView) view.findViewById(R.id.tv_car_num);
            viewHolder.carAddr = (TextView) view.findViewById(R.id.tv_car_addr);
            viewHolder.carDistance = (TextView) view.findViewById(R.id.tv_car_distance);
            viewHolder.carType = (ImageView) view.findViewById(R.id.iv_car_type_logo);
            viewHolder.carBrakeType = (ImageView) view.findViewById(R.id.iv_car_type_logo_info);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        CarInfo model = carList.get(position);

        List<CarPriceBean> carPrice = model.priceList;
        List<CarImageBean> carImage = model.carImgList;

        for (int i = 0; carPrice != null && i < carPrice.size() && carImage != null && i < carImage.size(); i++) {


            CarPriceBean carPriceBean = carPrice.get(i);
            CarImageBean carImageBean = carImage.get(i);
            String price = carPriceBean.unitPrice;
            String[] unit = price.split("\\,");

            String gearBox = model.gearBox;

            viewHolder.carPic.setImageURI(Uri.parse(UrlValues.CAR_IMG_ID + carImageBean.imgId));
            viewHolder.carName.setText(model.modelName);
            // viewHolder.carPrice.setText(unit[0]);
            viewHolder.carUnit.setText(unit[0]);

            viewHolder.carEndurance.setText(model.carKm);
            viewHolder.carNum.setText(model.licenseNo);
            viewHolder.carAddr.setText(model.addr);
            viewHolder.carDistance.setText(model.distance + "km");
            if (gearBox.equalsIgnoreCase("01")) {
                viewHolder.carType.setBackgroundResource(R.mipmap.icon_shou);
            } else if (gearBox.equalsIgnoreCase("02")) {
                viewHolder.carBrakeType.setBackgroundResource(R.mipmap.icon_zi);
            }
        }
        return view;
    }

    class ViewHolder {
        SimpleDraweeView carPic;
        TextView carName;
        TextView carPrice;
        TextView carUnit;
        TextView carEndurance;
        TextView carNum;
        TextView carAddr;
        TextView carDistance;
        ImageView carType;
        ImageView carBrakeType;
    }
}
